package com.HQCourse.vod.vod.service.impl;

import com.HQCourse.vod.vod.service.FileService;
import com.HQCourse.vod.vod.utils.ConstantPropertiesUtils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {
    @Override
    public String upload(MultipartFile file) {
        // 初始化用户身份信息(secretId, secretKey)
        String secretId = ConstantPropertiesUtils.ACCESS_KEY_ID;
        String secretKey = ConstantPropertiesUtils.ACCESS_KEY_SERET;
          COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
       // 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region(ConstantPropertiesUtils.END_POINT));
        // 配置使用 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
       // 生成 cos 客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        // Bucket 的命名格式为 BucketName-APPID ，此处填写的存储桶名称必须为此格式
        String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
       // 对象建是对象在存储桶的唯一标识
        String key = UUID.randomUUID().toString().replaceAll("-","")+file.getOriginalFilename();
        //根据当前日期上传文件
        String time = new DateTime().toString("yyyy/MM/dd");
        key=time+key;
        // 方法1 获取下载输入流
        try {
            InputStream inputStream = file.getInputStream();
            ObjectMetadata objectMetadata=new ObjectMetadata();
            PutObjectRequest putObjectRequest=new PutObjectRequest(bucketName,
                    key,
                    inputStream,
                    objectMetadata);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            String url="https://"+bucketName+"."+"cos"+"."+ConstantPropertiesUtils.END_POINT +".myqcloud.com"+"/"+key;

            return url;
            //https://zww-1318497587.cos.ap-nanjing.myqcloud.com/2023/623/1.jpg
             // 下载对象的 CRC64
             //            String crc64Ecma = cosObject.getObjectMetadata().getCrc64Ecma();
             //// 关闭输入流
             //            cosObjectInput.close();
             //
             //// 方法2 下载文件到本地的路径，例如 D 盘的某个目录
             //            String outputFilePath = "exampleobject";
             //            File downFile = new File(outputFilePath);
             //            getObjectRequest = new GetObjectRequest(bucketName, key);
             //            ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}
